package samples.d2.polygons 
{
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import samples.BaseSample;
	import shapes.shapes2D.distribution.Circular;
	import shapes.shapes2D.distribution.RandomDistribution;
	import shapes.shapes2D.polygon.ConvexHull;
	import shapes.shapes2D.polygon.Edge;
	import shapes.shapes2D.polygon.Intersection;
	import shapes.shapes2D.polygon.Polygon;
	import shapes.shapes2D.polygon.Skeleton;
	import shapes.shapes2D.polygon.Triangulation;
	
	/**
	 * @author Nicolas Barradeau
	 * http://en.nicoptere.net
	 */
	public class Polygon1 extends BaseSample
	{
		private var poly:Polygon;
		
		
		public function Polygon1() 
		{
			
			//poly = new Polygon( 0,0, RandomDistribution.distribute( new Rectangle( 100, 100, sw - 200, sh - 200 ), 500 ) );
			poly = new Polygon( 0,0, new Circular( new Point( sw/2, sh/2 ), 200, 200 ).distribute( 30, false, 60 ) );
			//addChild( poly );
			//poly.draw();
			
			poly = new Polygon( 0, 0,  poly.points );
			//ConvexHull.process(
			graphics.lineStyle( 10, 0x003399, 1 );
			plotLines( poly.points );
			
			graphics.lineStyle( 0, 0xFFFFFF );
			plotPoints( poly.points );
			
			plotLines( poly.points );
			//plotEdges( Skeleton.process( poly.edges, graphics ) );
			
			/*
			var p:Point = poly.centroid;
			poly.graphics.beginFill( 0xFF0000 );
			poly.graphics.drawCircle( p.x, p.y, 5 );
			
			graphics.lineStyle( 0, 0xFF0000 );
			plotPoints( poly.points );
			//*/
			
			/*
			plotLines( ConvexHull.process( poly.points ) );
			
			
			graphics.lineStyle( 2, 0xFFCC00 );
			var tris:Vector.<Point> = Triangulation.process( ConvexHull.process( poly.points ) )
			
			for ( var i:int = 0; i < tris.length; i += 3 )
			{
				graphics.moveTo( tris[i].x, tris[i].y );
				graphics.lineTo( tris[ i + 1 ].x, tris[ i + 1 ].y );
				graphics.lineTo( tris[ i + 2 ].x, tris[ i + 2 ].y );
				graphics.lineTo( tris[ i ].x, tris[ i ].y );
			}
			
			//addEventListener( Event.ENTER_FRAME, update );
			
			//*/
		}
		
		private function plotPoints( points:Vector.<Point>):void
		{
			
			var p:Point = points[ 0 ];
			for each( p in points )
			{
				graphics.drawCircle( p.x, p.y, 2 );
			}
			
		}
		
		private function plotLines( points:Vector.<Point>):void
		{
			if ( points.length == 0 ) return;
			var p:Point = points[ 0 ];
			graphics.moveTo( p.x, p.y );
			
			for each( p in points )
			{
				
				graphics.lineTo( p.x, p.y );
				
			}
			graphics.lineTo( points[ 0 ].x, points[ 0 ].y );
			
		}
		private function plotEdges( edges:Vector.<Edge>):void
		{
			if ( edges.length == 0 ) return;
			
			var e:Edge = edges[ 0 ];
			
			for each( e in edges )
			{
				
				graphics.moveTo( e.p0.x, e.p0.y );
				graphics.lineTo( e.p1.x, e.p1.y );
				
			}
			
		}
		
		private function update(e:Event):void 
		{
			graphics.clear();
			
			graphics.lineStyle( 10, 0x003399, 1 );
			var s0:Point = new Point( 100, 200 );
			var s1:Point = new Point( 500, 200 );
			graphics.moveTo( s0.x, s0.y );
			graphics.lineTo( s1.x, s1.y );
			
			var e0:Point = new Point( 250, 100 );
			var e1:Point = new Point( mouseX, mouseY );
			graphics.moveTo( e0.x, e0.y );
			graphics.lineTo( e1.x, e1.y );
			
			graphics.lineStyle( 0, 0xFFFFFF, 1 );
			var ip:Point = Intersection.lineIntersectLine( s0, s1, e0, e1 );
			if ( ip != null ) graphics.drawCircle( ip.x, ip.y, 10 );
			
		}
	}

}